package com.wanghaixin.controller;

import com.wanghaixin.dao.CarMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Car;
import com.wanghaixin.pojo.CarMake;
import com.wanghaixin.pojo.CarModel;
import com.wanghaixin.service.CarMakeService;
import com.wanghaixin.service.CarService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 19:47
 * @Description: 车款管理
 */
@Controller
public class CarController {
    @Autowired
    private CarService carService;

    //页面上需要品牌管理列表，这里引用
    @Autowired
    private CarMakeService carMakeService;

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:49  2020/11/2
     * @Version:  1.0.0
     * @Description:  分页查询
     */
    @RequestMapping("/sys/car/list")
    //不添加下面注解会报一下错误
    //Error resolving template [sys/make/list], template might not exist or might not be accessible by any of the configured Template Resolvers
    @ResponseBody
    public DataGridResult findByPage(QueryDTO queryDTO){
        return carService.findByPage(queryDTO);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:49  2020/11/2
     * @Version:  1.0.0
     * @Description:  删除
     */
    @RequestMapping("/sys/car/del")
    @ResponseBody
    public R delCar(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            carService.delCar(id);
        }
        return R.ok();
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date: 19:49  2020/11/2
     * @Version:  1.0.0
     * @Description:  调用品牌service层，获取所有类别，在添加时用
     */
    @RequestMapping("/sys/car/typeall")
    @ResponseBody
    public R typeall(){
        List<CarMake> all = carMakeService.findAll();
        return R.ok().put("sites",all);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date: 19:49  2020/11/2
     * @Version:  1.0.0
     * @Description:  id查询
     */
    @RequestMapping("/sys/car/info/{id}")
    @ResponseBody
    public R findById(@PathVariable("id") Integer id){
        Car byId = carService.findById(id);
        return R.ok().put("car",byId);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:49  2020/11/2
     * @Version:  1.0.0
     * @Description:  添加方法
     */
    @RequestMapping("/sys/car/save")
    @ResponseBody
    public R addCar(@RequestBody Car car){
        int i = carService.addCar(car);
        return i>0 ? R.ok():R.error("添加失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:49  2020/11/2
     * @Version:  1.0.0
     * @Description:  修改方法
     */
    @RequestMapping("/sys/car/update")
    @ResponseBody
    public R upCar(@RequestBody Car car){
        int i = carService.upCar(car);
        return i>0 ? R.ok():R.error("修改失败");
    }
}
